import { initState } from "./initState";
import { compileToFun } from "./complie/index";
import { mountComponent, callHook } from './lifecycle'
import { mergeOptions } from "./utils/index";
export function initMixin(Vue) {
    Vue.prototype._init = function (options) {
        const vm = this;
        // initGlobalApi进行了options初始化，不会为undefined
        vm.$options = mergeOptions(this.constructor.options, options);
        callHook(vm, 'beforeCreate')
        // 初始化状态
        initState(vm);
        callHook(vm, 'created')

        // 渲染模版
        if (vm.$options.el) {
            vm.$mount(vm.$options.el);
        }
    };

    // 处理dom节点挂载 render > template > el
    Vue.prototype.$mount = function (el) {
        const vm = this;
        let elDOM = document.querySelector(el);
        vm.$el = elDOM
        const options = vm.$options;
        if (!options.render) {
            const template = options.template;
            if (!template && el) {
                elDOM = elDOM.outerHTML;
                // 解析DOM
                let render = compileToFun(elDOM);

                options.render = render
            }
        }
        // 组件挂载
        mountComponent(vm, el)
    };
}
